<template>
  <div>
    <van-picker
  title="性别"
  show-toolbar
  :columns="columns"
  @confirm="onConfirm"
  @cancel="onCancel"
  @change="onChange"
  :default-index="value"
  item-height="20"
/>
  </div>
</template>

<script>
import { changeUser } from '@/api/user'
export default {
  props: {
    value: {
      type: Number,
      required: true
    }
  },
  data () {
    return {
      columns: ['男', '女'],
      gender: this.value
    }
  },
  methods: {
    async onConfirm () {
      try {
        await changeUser({
          gender: this.gender
        })
        this.$toast('修改成功')
        this.$emit('input', this.gender)
        this.$emit('close')
      } catch (err) {
        this.$toast('修改失败', err)
      }
    },
    async onChange (picker, value, index) {
      this.gender = index
    },
    onCancel () {
      this.$toast('取消')
      this.$emit('close')
    }
  }
}
</script>

<style>

</style>
